/*
 * Copyright (C) 2005, Velocityme Partnership
 * LicenceManagementActions.java
 *
 * Created on 18 June 2005, 02:22
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.utility.LicenceKey;
import com.velocityme.utility.ServerConfiguration;
import com.velocityme.www.actionforms.LicenceManagementActionForm;
import com.velocityme.www.dto.LicenceKeyDTO;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class LicenceManagementActions extends NodeActions {
    
    /** Creates a new instance of LicenceManagementActions */
    public LicenceManagementActions() {
    }
    
    public ActionForward showNode(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        LicenceKey[] keys = cs.getAllLicenceKeys();
        LicenceKeyDTO[] keyDTOs = new LicenceKeyDTO[keys.length];
        Date now = new Date();
        for (int i = 0; i < keys.length; i++) {
            Boolean expired = new Boolean(keys[i].getExpiryDate().before(now));
            keyDTOs[i] = new LicenceKeyDTO(expired, keys[i].getCreationDate(), keys[i].getExpiryDate(), keys[i].toString());
        }
        session.setAttribute("licenceKeys", keyDTOs);
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showAddLicence(ActionMapping mapping, 
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("clientGUID", ServerConfiguration.getInstance().getClientGUID());
        return mapping.findForward("ACTION_ADD_LICENCE");                                      
    }
    
    public ActionForward addLicence(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        LicenceManagementActionForm actionForm = (LicenceManagementActionForm)form;
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        if (request.getParameter("btnSubmit").equals("OK")) {
            cs.addEncodedLicence(actionForm.getEncodedLicence());
        }
        return mapping.findForward("ACTION_SHOW_NODE");
    }
    
    public ActionForward installLicence(ActionMapping mapping,
                                        ActionForm form,
                                        HttpServletRequest request,
                                        HttpServletResponse response) throws Exception {
        LicenceManagementActionForm actionForm = (LicenceManagementActionForm)form;
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        cs.addEncodedLicence(actionForm.getEncodedLicence());
        return mapping.findForward("ACTION_SHOW_LOGIN");
    }
    

    
}
